package com.main.prioritysms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PrioritySMS extends Activity {
	//Debug
	private static final String TAG = "SMS app";
	private static final boolean D = true;
	
	//Layouts
	private ListView mConvoView;
	
	//Database
	private TextDbAdapter mDbHelper;
	
	//Array Adapter
	private ArrayAdapter<String> mArrayAdapter;
	
	//Standard Var
	private static String DEFAULT_COMPOSE;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	if(D) Log.e(TAG, "+++ON CREATE+++");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox);
        mDbHelper = new TextDbAdapter(this);
        mDbHelper.open();
    	//Intent myIntent = new Intent();
    	//myIntent.setClassName("com.main.prioritysms", "com.main.prioritysms.TextSender");
    	//startActivity(myIntent);
        //fillData();
        setupInbox();
    }
    
    public void setupInbox(){
    	mArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
    	mConvoView = (ListView) findViewById(R.id.conversations);
    	mConvoView.setAdapter(mArrayAdapter);
    	DEFAULT_COMPOSE = "Compose a New Message";
    	
    	mArrayAdapter.add(DEFAULT_COMPOSE);
    	
    	mConvoView.setOnItemClickListener(new OnItemClickListener() {
    	    public void onItemClick(AdapterView<?> parent, View view,
    	            int position, long id) {
    	    	/*TODO edit the following to check the database for the name of the conversation to open
    	    	 * rather than simply running an intent on any click as its currently set up.
    	    	 */
    	    	Intent myIntent = new Intent();
    	    	myIntent.setClassName("com.main.prioritysms", "com.main.prioritysms.TextSender");
    	    	startActivity(myIntent);
    	        }
    	      });
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	/*TODO: Take the bundled information here, call filldata() and store the information
    	 * into the database.
    	 */
    }
    
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	mDbHelper.close();
    }
    /*
    @Override
    public void onStart(){
    	super.onStart();
    	if(D) Log.e(TAG, "+++ON START+++");
    }
    private void fillData() {
        Cursor notesCursor = mDbHelper.fetchAllTexts();
        startManagingCursor(notesCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{TextDbAdapter.KEY_TITLE};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.message};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter text = 
            new SimpleCursorAdapter(this, R.layout.message, notesCursor, from, to);
        setListAdapter(text);
    } */
}